<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="考试大类" prop="examTypeId">
        <el-select
          v-model="queryParams.examTypeId"
          placeholder="请选择考试大类"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in examTypeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:examslevel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:examslevel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:examslevel:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examslevelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考试大类" align="center" prop="examTypeName" :show-overflow-tooltip="true" />
      <el-table-column label="领域代码" align="center" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="领域中文名" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="能力/技能描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:examslevel:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:examslevel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:examslevel:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试等级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="考试大类" prop="examTypeId">
          <el-select
            v-model="form.examTypeId"
            placeholder="请选择考试大类"
            style="width: 100%"
          >
            <el-option
              v-for="item in examTypeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="领域代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入领域代码" />
        </el-form-item>
        <el-form-item label="领域中文名" prop="name">
          <el-input v-model="form.name" placeholder="请输入领域中文名" />
        </el-form-item>
        <el-form-item label="能力/技能描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入能力/技能描述" :rows="3" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 考试等级详情对话框 -->
    <el-dialog title="考试等级详情" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="考试大类">
          {{ examslevelInfo.examTypeName || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="领域代码">
          {{ examslevelInfo.code || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="领域中文名">
          {{ examslevelInfo.name || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="能力/技能描述" :span="2">
          {{ examslevelInfo.description || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">
          {{ examslevelInfo.remark || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建人">
          {{ examslevelInfo.createBy || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ parseTime(examslevelInfo.createTime) || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="更新人">
          {{ examslevelInfo.updateBy || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="更新时间">
          {{ parseTime(examslevelInfo.updateTime) || '暂无' }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEditFromDetail">编辑</el-button>
        <el-button @click="detailOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExamslevel, getExamslevel, delExamslevel, addExamslevel, updateExamslevel } from "@/api/system/examslevel"
import { listExamstype } from "@/api/system/examstype"

export default {
  name: "Examslevel",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试等级表格数据
      examslevelList: [],
      // 考试大类选项
      examTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 考试等级详情信息
      examslevelInfo: {},
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examTypeId: undefined,
        code: undefined,
        name: undefined,
        description: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examTypeId: [
          { required: true, message: "考试大类不能为空", trigger: "change" }
        ],
        code: [
          { required: true, message: "领域代码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "领域中文名不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getExamTypeOptions().then(() => {
      this.getList()
    })
  },
  methods: {
    /** 查询考试等级列表 */
    getList() {
      this.loading = true
      listExamslevel(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.examslevelList = response.rows
          this.total = response.total
          // 关联查询考试大类名称
          this.examslevelList.forEach(item => {
            const examType = this.examTypeOptions.find(type => type.id == item.examTypeId)
            item.examTypeName = examType ? examType.name : ''
          })
        console.log(this.examslevelList)
          this.loading = false
        }
      )
    },
    /** 获取考试大类选项 */
    getExamTypeOptions() {
      return listExamstype({pageNum:1,pageSize:1000}).then(response => {
        this.examTypeOptions = response.rows
        return response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        examTypeId: undefined,
        code: undefined,
        name: undefined,
        description: undefined,
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加考试等级"
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset()
      getExamslevel(row.id).then(response => {
        this.examslevelInfo = response.data
        // 设置考试大类名称显示
        const examType = this.examTypeOptions.find(item => item.id === this.examslevelInfo.examTypeId)
        this.examslevelInfo.examTypeName = examType ? examType.name : ''
        this.detailOpen = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExamslevel(id).then(response => {
        this.form = response.data
        this.form.examTypeId = this.form.examTypeId ? Number(this.form.examTypeId) : ''
        this.open = true
        this.title = "修改考试等级"
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateExamslevel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExamslevel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除考试等级编号为"' + ids + '"的数据项？').then(function() {
        return delExamslevel(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 从详情弹窗跳转到编辑弹窗 */
    handleEditFromDetail() {
      this.detailOpen = false
      this.form = { ...this.examslevelInfo }
      this.open = true
      this.title = "修改考试等级"
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/examslevel/export', {
        ...this.queryParams
      }, `examslevel_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
